package com.table;

import com.appclient.ClientWizardController;
import com.appclient.DaoInstanceEnum;
import com.carpark.ejb.Model.ClientsModel;
import java.util.ArrayList;
import java.util.List;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;

/**
 *
 * @author Sawior
 */
public class ClientTableController2 {

    @FXML
    private TextField filterField;
    @FXML
    private TableView<ClientsModel> clientTable;
    @FXML
    private TableColumn<ClientsModel, String> name;
    @FXML
    private TableColumn<ClientsModel, String> lastName;
    @FXML
    private TableColumn<ClientsModel, String> zipCode;
    @FXML
    private TableColumn<ClientsModel, String> city;
    @FXML
    private TableColumn<ClientsModel, String> street;
    @FXML
    private TableColumn<ClientsModel, String> houseNr;
    @FXML
    private TableColumn<ClientsModel, String> localNr;
    @FXML
    private TableColumn<ClientsModel, String> phone;
    @FXML
    private TableColumn<ClientsModel, String> nip;
    @FXML
    private Button choose;
    private ObservableList<ClientsModel> masterData = FXCollections.observableArrayList();
    private ObservableList<ClientsModel> filteredData = FXCollections.observableArrayList();
    private Stage dialogStage;
    private ClientWizardController wizard;
    private Integer selectedClient;

    public Integer getSelectedClient() {
        return selectedClient;
    }
    
  
    public ClientTableController2() {
        List<ClientsModel> data = DaoInstanceEnum.INSTANCE.getDao().getAllClients();
        for (ClientsModel client : data) {
            masterData.add(client);
        }

        filteredData.addAll(masterData);
        masterData.addListener(new ListChangeListener<ClientsModel>() {
            @Override
            public void onChanged(ListChangeListener.Change<? extends ClientsModel> change) {
                updateFilteredData();
            }
        });
    }
    
    

    /**
     * Initializes the controller class. This method is automatically called
     * after the fxml file has been loaded.
     */
    @FXML
    private void initialize() {
        name.setCellValueFactory(
                new PropertyValueFactory<ClientsModel, String>("name"));
        lastName.setCellValueFactory(
                new PropertyValueFactory<ClientsModel, String>("lastName"));
        zipCode.setCellValueFactory(
                new PropertyValueFactory<ClientsModel, String>("zipCode"));
        city.setCellValueFactory(
                new PropertyValueFactory<ClientsModel, String>("city"));
        street.setCellValueFactory(
                new PropertyValueFactory<ClientsModel, String>("street"));
        houseNr.setCellValueFactory(
                new PropertyValueFactory<ClientsModel, String>("houseNumber"));
        localNr.setCellValueFactory(
                new PropertyValueFactory<ClientsModel, String>("localNumber"));
        phone.setCellValueFactory(
                new PropertyValueFactory<ClientsModel, String>("phone"));
        nip.setCellValueFactory(
                new PropertyValueFactory<ClientsModel, String>("nipNumber"));

        clientTable.setItems(filteredData);

        filterField.textProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observable,
                    String oldValue, String newValue) {
                updateFilteredData();
            }
        });
    }

    /**
     * Updates the filteredData to contain all data from the masterData that
     * matches the current filter.
     */
    private void updateFilteredData() {
        filteredData.clear();

        for (ClientsModel p : masterData) {
            if (matchesFilter(p)) {
                filteredData.add(p);
            }
        }

        // Must re-sort table after items changed
        reapplyTableSortOrder();
    }

    /**
     * Returns true if the person matches the current filter. Lower/Upper case
     * is ignored.
     *
     * @param client
     * @return
     */
    private boolean matchesFilter(ClientsModel client) {
        String filterString = filterField.getText();
        if (filterString == null || filterString.isEmpty()) {
            return true;
        }
        String lowerCaseFilterString = filterString.toLowerCase();

        if (client.getName().toLowerCase().indexOf(lowerCaseFilterString) != -1) {
            return true;
        }
        if (client.getLastName().toLowerCase().indexOf(lowerCaseFilterString) != -1) {
            return true;
        }
        if (client.getCity().toLowerCase().indexOf(lowerCaseFilterString) != -1) {
            return true;
        }
        if (client.getStreet().toLowerCase().indexOf(lowerCaseFilterString) != -1) {
            return true;
        }
        Integer localNumber = client.getLocalNumber();
        if (localNumber != null) {
            if (localNumber.toString().toLowerCase().indexOf(lowerCaseFilterString) != -1) {
                return true;
            }
        }
        if (client.getHouseNumber().toLowerCase().indexOf(lowerCaseFilterString) != -1) {
            return true;
        }
        if (client.getZipCode().toLowerCase().indexOf(lowerCaseFilterString) != -1) {
            return true;
        }
        if (client.getPhone().toLowerCase().indexOf(lowerCaseFilterString) != -1) {
            return true;
        }

        return false;
    }

    public void setDialogStage(Stage dialogStage) {
        this.dialogStage = dialogStage;
    }

    private void reapplyTableSortOrder() {
        ArrayList<TableColumn<ClientsModel, ?>> sortOrder = new ArrayList<>(clientTable.getSortOrder());
        clientTable.getSortOrder().clear();
        clientTable.getSortOrder().addAll(sortOrder);
    }

    @FXML
    private void makeChoice(ActionEvent event) {
        if (event.getSource() == choose) {
            try{
                selectedClient = clientTable.getSelectionModel().getSelectedItem().getClientId();
            }
            catch(Exception e ){
            }
            
            if (selectedClient != null) {
                dialogStage.close();
            }
        }
    }

    public void setWizard(ClientWizardController wizard) {
        this.wizard = wizard;
    }

   
    
    
}
